package com.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.dao.ProductDAO;
import com.dao.SaleReturnDAO;
import com.dao.impl.ProductDAOImpl;
import com.dao.impl.SaleReturnDAOImpl;
import com.model.Product;
import com.model.SaleOrderItem;
import com.model.SaleReturn;
import com.model.SaleReturnItem;
import com.model.SaleReturnTemp;
import com.service.SaleReturnService;
import com.util.jsonUtil;

public class SaleReturnServiceImpl implements SaleReturnService {
	private SaleReturnDAO saleReturnDao = new SaleReturnDAOImpl();
	private ProductDAO productDao = new ProductDAOImpl();
	public int saleOrder_Id;

	public int getSaleOrder_Id() {
		return saleOrder_Id;
	}

	@Override
	public boolean addSaleReturn(SaleReturn saleReturn) {
		// TODO Auto-generated method stub
		return saleReturnDao.addSaleReturn(saleReturn);
	}

	@Override
	public boolean addSaleReturnItem(SaleReturnItem saleReturnItem) {
		// TODO Auto-generated method stub
		return saleReturnDao.addSaleReturnItem(saleReturnItem);
	}

	@Override
	public int getLastNum() {
		// TODO Auto-generated method stub
		return saleReturnDao.getLastNum();
	}

	@Override
	public String selectSaleItem(String saleOrderNo) {
		// TODO Auto-generated method stub
		List<SaleReturnTemp> saleReturnTempList = new ArrayList<SaleReturnTemp>();

		saleOrder_Id = saleReturnDao.findBySaleOrderNo(saleOrderNo);
		List<SaleOrderItem> saleOrderList = saleReturnDao
				.findBySaleOrder_Id(saleOrder_Id);
		for (int i = 0; i < saleOrderList.size(); i++) {

			SaleReturnTemp saleReturnTemp = new SaleReturnTemp();

			SaleOrderItem saleOrderItem = saleOrderList.get(i);

			int product_Id = saleOrderItem.getProduct_Id();
			Product product = productDao.getProduct(product_Id);

			saleReturnTemp.setId(i + 1);
			saleReturnTemp.setQuantity(saleOrderItem.getQuantity());
			saleReturnTemp.setSubtotal(saleOrderItem.getPrice());
			saleReturnTemp.setProductNo(product.getProductNo());
			saleReturnTemp.setProductName(product.getProductName());
			saleReturnTemp.setPrice(product.getPrice());
			saleReturnTempList.add(saleReturnTemp);
		}
		return jsonUtil.toJson(saleReturnTempList, saleReturnTempList.size());
	}
}
